package com.qianfeng.qlm.handforireader.fragment;

import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.qianfeng.qlm.handforireader.R;
import com.qianfeng.qlm.handforireader.activity.MainActivity;
import com.qianfeng.qlm.handforireader.activity.NewChapterActivity;
import com.qianfeng.qlm.handforireader.adapter.StickyListViewBaseAdapter;
import com.qianfeng.qlm.handforireader.model.NewBean;
import com.qianfeng.qlm.handforireader.model.NewBeanList;
import com.qianfeng.qlm.handforireader.util.Constant;
import com.qianfeng.qlm.handforireader.util.UrlMaker;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import se.emilsjolander.stickylistheaders.StickyListHeadersListView;

/**
 * Created by IntelliJ IDEA
 * User:tadhg
 * Date:2015/3/23
 * Email:tadhg299@163.com
 */
public class NewFragment extends Fragment implements AdapterView.OnItemClickListener {

    private RequestQueue requestQueue;
    private NewBeanList beanList;
    private JsonObjectRequest request;
    private ArrayList<NewBean> data;
    private StickyListViewBaseAdapter mAdapter;
    private List<NewBean> dataList;
    private MainActivity mainActivity;
    private RelativeLayout rlLoading;
    private ImageView imgLoading;
    private AnimationDrawable animationDrawable;

    public NewFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestQueue = Volley.newRequestQueue(getActivity());
        mainActivity = (MainActivity) getActivity();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_new, container, false);
        rlLoading = (RelativeLayout) view.findViewById(R.id.rl_loading);
        imgLoading = (ImageView) view.findViewById(R.id.new_loading_img);
        StickyListHeadersListView stickyListView = (StickyListHeadersListView) view.findViewById(R.id.fragment_sticky_list_view);
        data = new ArrayList<>();
        mAdapter = new StickyListViewBaseAdapter(getActivity(), data);
        stickyListView.setAdapter(mAdapter);
        stickyListView.setOnItemClickListener(this);
        initData();

//        String infor = UrlMaker.GetComicInfor("361");
//        Log.d("Testftggg",infor);


        return view;
    }

    @Override
    public void onResume() {
        super.onResume();

    }

    @Override
    public void onStop() {
        super.onStop();
        requestQueue.cancelAll(getActivity());
    }

    private void initData(){
        beanList = new NewBeanList();
        request = new JsonObjectRequest(
                Constant.URL_NEW,
                null,
                new  Response.Listener<JSONObject>(){
                    @Override
                    public void onResponse(JSONObject response) {
                       if (response != null){
                           animationDrawable.stop();
                           rlLoading.setVisibility(View.GONE);
                           beanList.parseJson(response);
                           dataList = beanList.getDataList();
                           mAdapter.setData(dataList);
                       }

                    }
                },
                new Response.ErrorListener(){

                    @Override
                    public void onErrorResponse(VolleyError error) {

                        error.printStackTrace();
                    }
                }
        );

        animationDrawable = (AnimationDrawable) imgLoading.getDrawable();
        animationDrawable.start();
        requestQueue.add(request);
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


        NewBean newBean = dataList.get(position);
        if (newBean != null){
            String commicId = newBean.getId();
            String url = UrlMaker.GetComicInfor(commicId);
            Intent intent = new Intent(mainActivity, NewChapterActivity.class);
            intent.putExtra("url",url);
            intent.putExtra("name",newBean.getComic_name());
            mainActivity.startActivity(intent);

        }

    }
}
